// pages/menu/personal.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

const app = getApp()
const cacheDateKey = 'today_is_login'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: wx.getStorageSync('isLogin') ? wx.getStorageSync('isLogin') : false,
        tipDialog: false,
        tipMsg: '',
        theme:'',
        loginsrc:'',
        huaweilogin:'',
        wode:'',
        indexpng:''
    },
    gettext(){
        let that = this;
        wx.request({
            url: app.globalData.domain + 'v4/index/gettext',
            success(res) {
                if (res.data.code == 0) {
                    that.setData({
                        text: res.data.data4,
                    });
                    
                }
            }
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var curDate = new Date();
        var curDateDetail = curDate.getFullYear() + '-' + curDate.getMonth() + '-' + curDate.getDate();
        var cacheDate = wx.getStorageSync(cacheDateKey);
        var cacheUserInfo = wx.getStorageSync('user_phone');
        var isChooseProtocal = wx.getStorageSync('isChooseProtocal')? wx.getStorageSync('isChooseProtocal'):false;
         //变量赋值
        this.setData({
            isChooseProtocal: isChooseProtocal,
        });
        if(!isChooseProtocal){
            wx.setStorageSync('toMenu','/pages/menu/personal');
        }
        this.gettext();
        if (!cacheDate || cacheDate != curDateDetail) {
            wx.setStorageSync(cacheDateKey, curDateDetail);
            wx.request({
                url: app.globalData.domain + 'index/index/logUserIp',
                data: {
                    'user_phone': cacheUserInfo.purePhoneNumber,
                },
                success: function (res) {
                    if (res.data.code != 0) {
                        wx.showModal({
                            title: '温馨提示',
                            content: '授权信息已过期,请重新授权进入',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    wx.navigateTo({
                                        url: '/pages/login/index',
                                    })
                                }
                            }
                        })
                    }
                }
            })
        }
        wx.showShareMenu({withShareTicket: true,menus: ['shareAppMessage', 'shareTimeline']});
    },
    onShareAppMessage() {
        return {title: '天纪云',path: '/pages/menu/personal'}
    },
    onShareTimeline() {
        return {title: '天纪云',path: '/pages/menu/personal'}
    },
    /**
     * 生命周期函数--监听页面显示
     */
     onShow: function () {
        if(wx.getStorageSync('theme') == 'style1'){
          this.setData({
            theme:'style1',
            loginsrc:'https://aiw.oss-cn-beijing.aliyuncs.com/logo.png',
            huaweilogin:'https://aiw.oss-cn-beijing.aliyuncs.com/huawei1.png',
            wode:'/images/wodepng.png',
            indexpng:'/images/indexpng.png',
            gypng:'/images/gypng.png'
          })
        }else{
          this.setData({
            theme:'init',
            loginsrc:'https://aiw.oss-cn-beijing.aliyuncs.com/logo.png',
            huaweilogin:'https://aiw.oss-cn-beijing.aliyuncs.com/huawei1.png',
            wode:'https://aiw.oss-cn-beijing.aliyuncs.com/v3/tab_me_n@2x.png',
            indexpng:'https://aiw.oss-cn-beijing.aliyuncs.com/v3/tab_home_s@2x.png',
            gypng:'/images/ai.png'
          })
        }
        this.setData({
            isLogin: wx.getStorageSync('isLogin')
          })
        setTimeout(() => {
            app.show(this, 'show', 1)
        }, 1000);
    },

    onHide: function () {
        setTimeout(() => {
            app.show(this, 'show', 0)
        }, 1000);
    },
    getPhoneNumber(e) {
        console.log(e);
        let {
            encryptedData,
            iv,
            code
        } = e.detail
        // 如果授权获取手机号则进入下个页面 
        if (code) {
            this.getPhone(code)
        } else {
            Dialog.alert({
                title: '温馨提示',
                message: '请授权登录',
                theme: 'round-button',
            }).then(() => {
                // on close
            });
        }
    },

    // 用户获取手机号
    getPhone(code) {
        let that = this
        var reqUri = app.globalData.domain + 'index/index/getUserWxPhone';
        wx.request({
            url: reqUri,
            method: 'POST',
            data: {
                code,
            },
            success(res) {
                console.log(res)
                if (res.data.code == app.globalData.successCode) {
                    let {
                        phoneNumber
                    } = res.data.data
                    // 将用户的 手机号缓存
                    wx.setStorageSync('user_phone', res.data.data)
                    that.login(phoneNumber)
                } else {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg
                    })
                    return ;                    
                }
            }
        })
    },

    // 用户登录
    login(phone) {
        let that = this
        var reqUri = app.globalData.domain + 'index/index/doReg';
        var uid =  wx.getStorageSync('uid')??0;
        let cid3 = wx.getStorageSync('tocid3');

        if (!cid3 || cid3 == undefined) cid3 = that.data.cid3;
        wx.request({
            url: reqUri,
            method: 'POST',
            data: {
                phone,
                types: 'mini',
                openid: app.globalData.openId,
                uid:uid,
                cid3:cid3,
                agentId: wx.getStorageSync('agentId') ?? 1
            },
            success(res) {
                console.log(res)
                if (res.data.code == app.globalData.successCode) {
                    Toast.success('登录成功,即将跳转')
                    wx.setStorageSync('isLogin', true)
                    wx.redirectTo({
                      url: '/pages/personal/me',
                  })
                } else {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg
                    })
                    return ;                    
                }
            }
        })
    },
    toDetail(e) {
        let {
            type,
            cid3
        } = e.currentTarget.dataset
        wx.navigateTo({
            url: '/pages/newDetail/indexV2?type=' + type + '&cid3=' + cid3,
        })
    },
    /**
     * 至首页
     */
    toHome() {
        wx.navigateTo({
            url: '/pages/menu/index',
        })
    },
    /**
     * 至个人中心
     */
    toMe() {
        wx.navigateTo({
            url: '/pages/personal/me',
        })
    },
    /**
     * 至个人中心
     */
    toGroupMenu() {
        wx.navigateTo({
            url: '/pages/menu/group',
        })
    },
    /**
     * 至个人中心
     */
    toPersonalMenu() {
        wx.navigateTo({
            url: '/pages/menu/personal',
        })
    },
      /**
   * 至授权页
   */
       toLogin() {
        wx.redirectTo({
          url: '/pages/login/index',
        })
      },
})